package com.whfc.wxmp.api.mach.v1.controller;

import com.whfc.common.result.PageData;
import com.whfc.common.result.R;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.JSONUtil;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.env.dto.EnvWarnCountDTO;
import com.whfc.fuum.entity.WxUser;
import com.whfc.mach.dto.warn.AppMachWarnCountDTO;
import com.whfc.mach.dto.warn.AppMachWarnCountDTOV2;
import com.whfc.mach.dto.warn.AppWarnDetailDTO;
import com.whfc.mach.dto.warn.AppWarnMachRecordDTO;
import com.whfc.mach.param.warn.AppWarnHandleParam;
import com.whfc.wxmp.api.mach.v1.service.WxmpMachWarnService;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author sun_guodong
 * @description 小程序设备报警
 * @date 2020-12-14
 */
@RestController
@RequestMapping("/mp/api/v1/mach/warn")
public class WxmpMachWarnController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxmpMachWarnService wxmpMachWarnService;
    /**
     * 2.0 报警记录列表
     * @param wxUser
     * @param deptId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    public R<PageData<AppWarnMachRecordDTO>> machWarnList(@CurrentWxUser WxUser wxUser,
                               @RequestParam("deptId") Integer deptId,
                               @RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize,
                               @RequestParam(value="state",required = false) Integer state,
                               @RequestParam(value="ruleType",required = false) Integer ruleType,
                               @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                               @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取设备报警记录列表", RequestConetxtUtil.getParameters());
        PageData<AppWarnMachRecordDTO> pageData = wxmpMachWarnService.machWarnList(deptId, pageNum, pageSize,state,startTime,endTime,ruleType);
        return R.success(pageData);
    }

    /**
     * 查询设备报警消息详情
     *
     * @param loginUser
     * @param warnId
     * @return
     */
    @GetMapping("detail")
    public Result getMachWarnMsgDetail(@CurrentWxUser WxUser loginUser,
                                       @RequestParam("warnId") Integer warnId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询设备报警消息详情", RequestConetxtUtil.getParameters());
        AppWarnDetailDTO warnDetail = wxmpMachWarnService.getMachWarnDetail(warnId);
        return ResultUtil.success(warnDetail);
    }

    /**
     * 处理报警消息
     *
     * @param loginUser
     * @param body
     * @return
     */
    @PostMapping("handle")
    public Result handleMachWarn(@CurrentWxUser WxUser loginUser,
                                 @RequestBody @Validated AppWarnHandleParam body) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "处理设备报警消息", JSONUtil.toString(body));
        wxmpMachWarnService.handleMachWarn(loginUser, body);
        return ResultUtil.success();
    }
    /**
     * 2.0 报警数量统计
     * @param user
     * @param deptId

     * @return
     */

    @GetMapping("/count")
    public R<AppMachWarnCountDTOV2> warnCount(@CurrentWxUser WxUser user,
                                        @RequestParam("deptId") Integer deptId,
                                        @RequestParam(value = "startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                        @RequestParam(value = "endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        logger.info("报警记录统计,loginer:{},deptId:{},startTime:{},endTime:{}",
                user.getName(), deptId, startTime, endTime);
        AppMachWarnCountDTOV2 envWarnCountDTO = wxmpMachWarnService.warnNumStatic(deptId, startTime, endTime);
        return R.success(envWarnCountDTO);
    }
}
